﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;
using WebApplication1.domain;

namespace WebApplication1.dormitoryAdministrator
{
    public partial class StudentStayManagement : System.Web.UI.Page
    {
        List<DormitoryInfo> dormitoryInfos = new List<DormitoryInfo>();
        String hostel;
        protected void Page_Load(object sender, EventArgs e)
        {
            hostel = (String)Application["hostel"];
            if (!Page.IsPostBack)
            {

                this.getList();
            }
        }
        private void bind(DataTable Adt)
        {
            int row = Adt.Rows.Count;
            for (int r = 0; r < row; r++)
            {
                DormitoryInfo dormitoryInfo = new DormitoryInfo();
                List<domain.Student> students = new List<domain.Student>();
                List<String> studentNames = new List<String>();
                dormitoryInfo.Id = (int)Adt.Rows[r][0];
                dormitoryInfo.Name = Adt.Rows[r][1].ToString();
                dormitoryInfo.Hostel_id = (int)Adt.Rows[r][2];
                String sql = "select name from hostel where id = '" + dormitoryInfo.Hostel_id + "'";
                DataTable data = MySqlHelper.GetDataTable(sql);
                //2.获取第一行数据
                dormitoryInfo.HostelName = data.Rows[0][0].ToString();
                String sql2 = "select name from student where dormitory_id = '" + dormitoryInfo.Id + "'";
                DataTable studentsTable = MySqlHelper.GetDataTable(sql2);
                int row1 = studentsTable.Rows.Count;

                for (int r1 = 0; r1 < row1; r1++)
                {
                    domain.Student student = new domain.Student();
                    student.Name = studentsTable.Rows[r1][0].ToString();
                    studentNames.Add(studentsTable.Rows[r1][0].ToString());
                    students.Add(student);
                }
                dormitoryInfo.StudentNames = studentNames;
                dormitoryInfos.Add(dormitoryInfo);
            }
            GridView1.DataSource = dormitoryInfos;
            GridView1.DataBind();
        }
        private void getList()
        {
            String sql = "SELECT * FROM `dormitory` d LEFT JOIN hostel h on d.hostel_id = h.id where h.`name` = '" + hostel + "'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
            // DataRow row = MySqlHelper.GetDataRow(sql);
        }

        protected void s_Click(object sender, EventArgs e)
        {
            string name = selectName.Text;
            String sql = "SELECT * FROM `dormitory` d LEFT JOIN hostel h on d.hostel_id = h.id where dormitory_number like '%" + name + "%' and h.`name` = '" + hostel + "'";
            DataTable Adt = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            bind(Adt);
        }

        protected void selectAll_Click(object sender, EventArgs e)
        {
            getList();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.getList();
        }
    }
}